package com.haikesi.api.excutionplan.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.device.domain.vo.DeviceSearchVO;
import com.haikesi.api.device.domain.vo.DeviceVO;
import com.haikesi.api.device.service.IDeviceService;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeSearchVO;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeVO;
import com.haikesi.api.devicetype.service.IDeviceTypeService;
import com.haikesi.api.excutionplan.domain.po.ExcutionPlanPO;
import com.haikesi.api.excutionplan.domain.vo.*;
import com.haikesi.api.excutionplan.service.IExcutionPlanService;
import com.haikesi.api.mode.domain.vo.ModeSearchVO;
import com.haikesi.api.mode.domain.vo.ModeVO;
import com.haikesi.api.mode.service.IModeService;
import com.haikesi.api.subscribertable.domain.vo.SubscriberTableSearchVo;
import com.haikesi.api.subscribertable.domain.vo.SubscriberTableVo;
import com.haikesi.api.subscribertable.service.ISubscriberTableService;
import com.haikesi.api.timertask.domain.po.TimerTaskPO;
import com.haikesi.api.timertask.domain.vo.*;
import com.haikesi.api.timertask.service.ITimerTaskService;
import com.haikesi.util.ButlerApiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "执行计划相关接口", tags = ApiIndex.ExcutionPlanController)
@RequestMapping(value = "/api/exeutionPlan")
@RestController
public class ExcutionPlanController {

    @Autowired
    private IExcutionPlanService service;

    @Autowired
    private ISubscriberTableService subscriberTableService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IModeService modeService;

    @ApiOperation(value = "查询执行计划列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<ExcutionPlanVO>>> list(@ApiParam(value = "查询参数") @ModelAttribute ExcutionPlanSearchVO excutionPlanSearchVO) {
        List<ExcutionPlanVO> list = service.getList(excutionPlanSearchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "添加执行计划")
    @PostMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R add(@ApiParam(value = "添加参数") @Valid @RequestBody ExcutionPlanAddVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        ExcutionPlanPO excutionPlanPO = service.addExcutionPlan(vo);
        return new R(excutionPlanPO);
    }

    @ApiOperation(value = "修改执行计划")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R updateTimerTask(@ApiParam(value = "修改参数") @Valid @RequestBody ExcutionPlanUpdateVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.updateExcutionPlan(vo);
        return new R();
    }

    @ApiOperation(value = "删除执行计划")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(@ApiParam(value = "执行计划ID") @RequestParam String id) {
        service.delete(id, "");
        return new R();
    }

    @ApiOperation(value = "查询设备类型")
    @GetMapping(value = "/getDriverType")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<Object> get(@ApiParam(value = "查询参数") String productCode) {
        String s = ButlerApiUtils.queryPlanDriverCondition(productCode);
        return new R(s);
    }

    @ApiOperation(value = "查询设备类型cmd")
    @GetMapping(value = "/getDriverTypeByCmd")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<Object> getDriverTypeByCmd(@ApiParam(value = "查询参数") String productCode) {
        String s = ButlerApiUtils.queryPlanDriverCondition(productCode);
        return new R(s);
    }


    //3D相关接口
    @ApiOperation(value = "通过组织id查询组织授权的账户接口")
    @PostMapping(value = "/sbInter/getSmhlist")
    public JSONObject getSmhlist(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {
        System.out.println(excutionPlan3DVO.getOrg_id());
        List<Map<String, String>> listMap = new ArrayList<>();
        SubscriberTableSearchVo subscriberTableSearchVo = new SubscriberTableSearchVo();
        subscriberTableSearchVo.setPageSize(999);
//        List<SubscriberTableVo> list = subscriberTableService.getList(subscriberTableSearchVo);
        List<SubscriberTableVo> list = subscriberTableService.getSmhlist();
        for (SubscriberTableVo sub : list) {
            Map<String, String> map = new HashMap<>();
            map.put("smarthome_account", sub.getSubscriberNumber());
            listMap.add(map);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        jsonObject.put("data", listMap);
        return jsonObject;
    }

    @ApiOperation(value = "通过账户、组织获取授权的设备类型")
    @PostMapping(value = "/sbInter/getSbLxlist")
    public JSONObject getSbLxlist(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {

        System.out.println("account:" + excutionPlan3DVO.getAccount());
        System.out.println("org_id:" + excutionPlan3DVO.getOrg_id());

        List<Map<String, String>> listMap = new ArrayList<>();
        DeviceTypeSearchVO vo = new DeviceTypeSearchVO();
        vo.setPageSize(9999);
        List<DeviceTypeVO> list = deviceTypeService.getList(vo);
        for (DeviceTypeVO obj : list) {
            Map<String, String> map = new HashMap<>();
            map.put("mlmc", obj.getTypeName());
            map.put("mldm", obj.getProductCode());
            listMap.add(map);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        jsonObject.put("data", listMap);
        return jsonObject;
    }

    @ApiOperation(value = "通过账户、组织可以查询所有类型的设备\n" +
            "通过账户、组织、类型可以查询指定类型的设备\n" +
            "通过MAC可以查询唯一指定的设备")
    @PostMapping(value = "/sbInter/getSblist")
    public JSONObject getSblist(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {

        DeviceSearchVO vo = new DeviceSearchVO();
        if (excutionPlan3DVO.getPageNo() != null) {
            vo.setPageNum(Integer.parseInt(excutionPlan3DVO.getPageNo()));
        }
        if (excutionPlan3DVO.getPageSize() != null) {
            vo.setPageSize(Integer.parseInt(excutionPlan3DVO.getPageSize()));
        }
//        vo.setProductCode(excutionPlan3DVO.getCode());
        vo.setDeviceName(excutionPlan3DVO.getName());
        vo.setSerialNum(excutionPlan3DVO.getNum());
//        vo.setDeviceSubscriberNumber(excutionPlan3DVO.getAccount());
        List<DeviceVO> list1 = deviceService.getList(vo);
        List<Map<String, String>> listMap = new ArrayList<>();
        PageInfo pageInfo = new PageInfo(list1);
        for(DeviceVO obj : list1){
            Map<String, String> map = new HashMap<>();
            map.put("device_name", obj.getDeviceName());
            map.put("device_mac", obj.getDeviceMac());
            map.put("device_num", obj.getProductCode());
            map.put("total", pageInfo.getTotal() + "");
            listMap.add(map);
        }

        /*List<Map<String, String>> listMap = new ArrayList<>();
        DeviceTypeSearchVO vo = new DeviceTypeSearchVO();
        vo.setPageSize(9999);
        List<DeviceTypeVO> list = deviceTypeService.getList(vo);
        for (DeviceTypeVO obj : list) {
            Map<String, String> map = new HashMap<>();
            map.put("mlmc", obj.getTypeName());
            map.put("mldm", obj.getProductCode());
            listMap.add(map);
        }*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("data", listMap);
        return jsonObject;
    }

    @ApiOperation(value = "通过账户、组织查询单账户的模式")
    @PostMapping(value = "/sbInter/getZhms")
    public JSONObject getZhms(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {
        ModeSearchVO vo = new ModeSearchVO();
        vo.setPageSize(9999);
        List<ModeVO> list1 = modeService.getList(vo);
        List<Map<String, String>> listMap = new ArrayList<>();
        for(ModeVO obj : list1){
            Map<String, String> map = new HashMap<>();
            map.put("sbmogl_name", obj.getModeName());
            map.put("sbmogl_ymsid", obj.getPlatformModeId());
            listMap.add(map);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        jsonObject.put("data", listMap);

        return jsonObject;
    }

    @ApiOperation(value = "通过账户、计划名称、状态、用户、组织添加执行计划")
    @PostMapping(value = "/sbInter/addZxjh")
    public JSONObject addZxjh(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {

        System.out.println("account：" + excutionPlan3DVO.getAccount());
        System.out.println("mc：" + excutionPlan3DVO.getMc());
        System.out.println("zt：" + excutionPlan3DVO.getZt());
        System.out.println("user_id：" + excutionPlan3DVO.getUser_id());
        System.out.println("org_id：" + excutionPlan3DVO.getOrg_id());

        ExcutionPlanAddVO vo = new ExcutionPlanAddVO();
        vo.setAccount(excutionPlan3DVO.getAccount());
        vo.setExcutionPlanName(excutionPlan3DVO.getMc());
        vo.setExcutionPlanEnable(2);

        /*JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        jsonObject.put("yid", "333");*/

        ExcutionPlanPO excutionPlanPO = service.addExcutionPlan(vo);
        JSONObject jsonObject = new JSONObject();
        if (excutionPlanPO != null) {
            jsonObject.put("resultCode", 200);
            jsonObject.put("resultDesc", "");
            jsonObject.put("yid", excutionPlanPO.getPlatformId());
        }
        return jsonObject;
    }

    @ApiOperation(value = "通过账户、计划名称、状态、用户、组织、计划ID、云计划ID修改执行计划")
    @PostMapping(value = "/sbInter/editZxjh")
    public JSONObject editZxjh(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {

        System.out.println("yid：" + excutionPlan3DVO.getYid());
        System.out.println("account：" + excutionPlan3DVO.getAccount());
        System.out.println("mc：" + excutionPlan3DVO.getMc());
        System.out.println("zt：" + excutionPlan3DVO.getZt());
        System.out.println("user_id：" + excutionPlan3DVO.getUser_id());
        System.out.println("org_id：" + excutionPlan3DVO.getOrg_id());

        /*ExcutionPlanUpdateVO vo = new ExcutionPlanUpdateVO();
        vo.setAccount(excutionPlan3DVO.getAccount());
        vo.setExcutionPlanName(excutionPlan3DVO.getMc());
        vo.setExcutionPlanEnable(Integer.parseInt(excutionPlan3DVO.getZt()));
        vo.setPlatformId(excutionPlan3DVO.getYid());
        service.updateExcutionPlan(vo);*/

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        return jsonObject;
    }

    @ApiOperation(value = "通过账户、计划名称、状态、用户、组织、计划ID、云计划ID修改执行计划")
    @PostMapping(value = "/sbInter/delZxjh")
    public JSONObject delZxjh(@RequestBody ExcutionPlan3DVO excutionPlan3DVO) {

        System.out.println("yid：" + excutionPlan3DVO.getYid());
        System.out.println("account：" + excutionPlan3DVO.getAccount());

//        service.delete(yid, account);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("resultCode", 200);
        jsonObject.put("resultDesc", "");
        return jsonObject;
    }


}
